package com.bilicute.libman.controller;

import com.bilicute.libman.pojo.Book;
import com.bilicute.libman.pojo.PageBean;
import com.bilicute.libman.pojo.Record;
import com.bilicute.libman.pojo.Result;
import com.bilicute.libman.quickMethods.QuickMethods;
import com.bilicute.libman.service.BookService;
import com.bilicute.libman.service.RecordService;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * @所属包名: com.bilicute.libman.controller
 * @类名: BookController
 * @作者: i囡漫笔
 * @描述: 图书互动接口
 * @创建时间: 2024-06-21 13:41
 */
@Validated
@CrossOrigin
@RestController
@RequestMapping("/api/book")
public class BookController {

    private BookService bookService;
    @Autowired
    private void setUserService(BookService bookService){
        this.bookService=bookService;
    }

    private RecordService recordService;
    @Autowired
    public void setRecordService(RecordService recordService) {
        this.recordService = recordService;
    }

    @PostMapping("/add")
    public Result<String> add(@Valid Book book){
        if (!QuickMethods.isAdmin()) return Result.error("该操作只有管理员才可以执行");
        if (book == null) return Result.error("添加失败，没有获取到添加的书籍");
        bookService.add(book);
        return Result.success();
    }

    @PatchMapping("/off")
    public Result<String> off(@NotNull Integer id){
        if (!QuickMethods.isAdmin()) return Result.error("该操作只有管理员才可以执行");
        Book bookById = bookService.findBookById(id);
        if (bookById == null) return Result.error("下架失败，没有获取到要下架的书籍");
        if (!bookById.getBook_status().equals("0")) return Result.error("下架失败，该图书暂未被归还");
        bookService.off(id);
        return Result.success();
    }

    @PatchMapping("/pass")
    public Result<String> pass(@NotNull Integer id){
        if (!QuickMethods.isAdmin()) return Result.error("该操作只有管理员才可以执行");
        Book bookById = bookService.findBookById(id);
        if (bookById == null) return Result.error("通过审查失败，没有获取到要归还的书籍");
        if (!bookById.getBook_status().equals("2")) return Result.error("审核失败，该图书目前无需审核");
        recordService.returnBook(bookById.getRecord_id_in_book());
        bookService.pass(id);
        return Result.success();
    }

    @GetMapping("/find")
    public Result<Book> findBookById(@Valid @NotNull Integer id){
        return Result.success(bookService.findBookById(id));
    }

    @DeleteMapping("/delete")
    public Result<String> delete(Integer id){
        if (id == null) return Result.error("删除失败，未知ID");
        Book bookById = bookService.findBookById(id);
        if (bookById == null) return Result.error("删除失败，未找到书籍");
        if (!bookById.getBook_status().equals("3")) return Result.error("删除失败，只有已下架的书籍才能被删除");
        if (!QuickMethods.isAdmin()) return Result.error("删除失败，没有管理员权限");
        bookService.delete(id);
        return Result.success();
    }

    @PatchMapping("/borrow")
    public Result<String> borrow(Integer id){
        System.out.println(id);
        Book bookById = bookService.findBookById(id);
        if (bookById == null) return Result.error("借阅失败，找不到借阅图书");
        if (!bookById.getBook_status().equals("0")) return Result.error("借阅失败，图书正在别其他人借阅中或已下架");
        Integer loggedInUserId = QuickMethods.getLoggedInUserId();
        String recordID = recordService.newRecord(bookById,loggedInUserId);
        bookService.borrow(id,loggedInUserId,recordID);

        return Result.success();
    }
    @PatchMapping("/result")
    public Result<String> resultBook(Integer id){
        Book bookById = bookService.findBookById(id);
        if (bookById == null) return Result.error("归还失败，找不到归还图书");
        if (!bookById.getBook_status().equals("1")) return Result.error("归还失败，当前图书不需要归还或等待管理员审核");
        Integer loggedInUserId = QuickMethods.getLoggedInUserId();
        if (!bookById.getBook_borrower().equals(loggedInUserId)) return Result.error("归还失败，您不是借阅人");
        bookService.resultBook(id);
        return Result.success();
    }

    @GetMapping
    public Result<PageBean<Book>> list(
            //当前页码
            Integer pageNum,
            //每页条数
            Integer pageSize,
            //文章ID
            @RequestParam(required = false) Integer book_status
    ){
        if (pageNum == null&&pageSize==null) return Result.error("关键数据为空");
        PageBean<Book> bookPageBean = bookService.list(pageNum,pageSize,book_status);
        return Result.success(bookPageBean);
    }


    @GetMapping("/oneself")
    public Result<PageBean<Book>> listMyBorrow(
            //当前页码
            Integer pageNum,
            //每页条数
            Integer pageSize,
            //文章ID
            @RequestParam(required = false) Integer book_status
    ){
        if (pageNum == null||pageSize==null) return Result.error("关键数据为空");
        Integer loggedInUserId = QuickMethods.getLoggedInUserId();
        PageBean<Book> bookPageBean = bookService.listMyBorrow(pageNum,pageSize,book_status,loggedInUserId);
        return Result.success(bookPageBean);
    }
}
